package com.hdyg.csc.mvp.base;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;

/**
 * 代理类 基类
 *
 * @author 陈白衣
 * @time 2018/2/3 14:28
 */
public abstract class BasePresenter<T> {
    public String status_code;
    public String message;

    protected Reference<T> mViewRef; // View 接口类型弱引用

    /**
     * 建立关联
     *
     * @param view
     */
    public void attachView(T view) {
        mViewRef = new WeakReference<T>(view);  // 建立关联
    }

    /**
     * 获取View
     *
     * @return
     */
    protected T getView() {
        return mViewRef.get();
    }

    /**
     * 判断是否与View 建立关联
     *
     * @return
     */
    public boolean isViewAttached() {
        return mViewRef != null && mViewRef.get() != null;
    }

    /**
     * 解除关联
     */
    public void detachView() {
        if (mViewRef != null) {
            mViewRef.clear();
            mViewRef = null;
        }
    }

}
